package com.kma.summer2012.dao;

import com.kma.summer2012.factory.HibernateUtil;
import com.kma.summer2012.model.CrumbsRequest;
import org.hibernate.Session;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Администратор
 * Date: 15.01.13
 * Time: 18:58
 * To change this template use File | Settings | File Templates.
 */
public class CrumbsRequestDAOImpl implements DAO<CrumbsRequest>{
    @Override
    public void add(CrumbsRequest request) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(request);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public void update(CrumbsRequest request) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(request);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public CrumbsRequest getById(int request_id) {
        Session session = null;
        CrumbsRequest request = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            request = (CrumbsRequest) session.get(CrumbsRequest.class, request_id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return request;
    }

    @Override
    public Collection<CrumbsRequest> getAll() {
        Session session = null;
        List requests = new ArrayList<CrumbsRequest>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            requests = session.createCriteria(CrumbsRequest.class).list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return requests;
    }

    @Override
    public void delete(CrumbsRequest request) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(request);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }
}
